// pages/trade/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dis:false,    // 兑换的模态框
    isconfirm: false,   //判断确定按钮的颜色

    //  input的data-name
    user_name: '',
    user_phone: '',
    province: '',
    city: '',
    area: '',
    user_address: '',

    banners: [
      { img: '../../images/u213.png', title: 'CAMEL骆驼户外情侣款冲锋衣', money: '299.00', score: '900', shu: '12', },
    ],
    talks: '说明',

    detail: {
      user_name: '',
      user_phone: '',
      province: '',
      city: '',
      area: '',
      address: ''
    },
  },

  //   失去光标时获取

  close_input: function (e) {
    var that = this;
    if (e.currentTarget.dataset.name == "user_name") {
      that.setData({user_name_blur: false });
    }
    if (e.currentTarget.dataset.name == "user_phone") {
      that.setData({user_name_blur1: false });
    }
    if (e.currentTarget.dataset.name == "province") {
      that.setData({ user_name_blur2: false });
    }
    if (e.currentTarget.dataset.name == "city") {
      that.setData({user_name_blur3: false });
    }
    if (e.currentTarget.dataset.name == "area") {
      that.setData({user_name_blur4: false });
    }
    if (e.currentTarget.dataset.name == "user_address") {
      that.setData({user_name_blur5: false });
    }
  },
  // 当input获取焦点的时候
  focus_input: function (e) {
    var that = this;
    if (e.currentTarget.dataset.name == "user_name") {
      that.setData({ user_name_blur: true });
    }
    if (e.currentTarget.dataset.name == "user_phone") {
      that.setData({ user_name_blur1: true });
    }
    if (e.currentTarget.dataset.name == "province") {
      that.setData({ user_name_blur2: true });
    }
    if (e.currentTarget.dataset.name == "city") {
      that.setData({ user_name_blur3: true });
    }
    if (e.currentTarget.dataset.name == "area") {
      that.setData({ user_name_blur4: true });
    }
    if (e.currentTarget.dataset.name == "user_address") {
      that.setData({ user_name_blur5: true });
    }
  },


  //   监听icon图
  enter_input: function (e) {
    var that = this;
    if (e.currentTarget.dataset.name == "user_name") {
      that.setData({ user_name: e.detail.value, 'detail.user_name': e.detail.value, user_name_blur: true });
    }
    if (e.currentTarget.dataset.name == "user_phone") {
      that.setData({ user_phone: e.detail.value, 'detail.user_phone': e.detail.value });
    }
    if (e.currentTarget.dataset.name == "province") {
      that.setData({ province: e.detail.value, 'detail.province': e.detail.value });
    }
    if (e.currentTarget.dataset.name == "city") {
      that.setData({ city: e.detail.value, 'detail.city': e.detail.value });
    }
    if (e.currentTarget.dataset.name == "area") {
      that.setData({ area: e.detail.value, 'detail.area': e.detail.value });
    }
    if (e.currentTarget.dataset.name == "user_address") {
      that.setData({ user_address: e.detail.value, 'detail.user_address': e.detail.value });
    }
    var lock = false, num_lod = 0;
    for (var i in that.data.detail) {
      if (that.data.detail[i] != '') {
        ++num_lod;
      }
    }
    if (num_lod == 6) {
      lock = true;
    }
    that.setData({
      isconfirm: lock
    })
  },
  //   清楚当条input数据
  delete_icon: function (e) {
    console.log(e);
    var that = this;
    if (e.currentTarget.dataset.name == "user_name") {
      that.setData({ user_name: '', 'detail.user_name': '' });
    }
    if (e.currentTarget.dataset.name == "user_phone") {
      that.setData({ user_phone: '', 'detail.user_phone': '' });
    }
    if (e.currentTarget.dataset.name == "province") {
      that.setData({ province: '', 'detail.province': '' });
    }
    if (e.currentTarget.dataset.name == "city") {
      that.setData({ city: '', 'detail.city': '' });
    }
    if (e.currentTarget.dataset.name == "area") {
      that.setData({ area: '', 'detail.area': '' });
    }
    if (e.currentTarget.dataset.name == "user_address") {
      that.setData({ user_address: '', 'detail.user_address': '' });
    }
    var lock = false, num_lod = 0;
    for (var i in that.data.detail) {
      if (that.data.detail[i] != '') {
        ++num_lod;
      }
    }
    if (num_lod == 6) {
      lock = true;
    }
    that.setData({
      isconfirm: lock
    })
  },

  //   立即兑换的模态框
  aaaa: function () {
    var that = this;
    var dis = that.data.dis;
    that.setData({
      dis: true
    })
  },
  // 确定兑换的模态框
  deletes: function (e) {
    console.log(e);
    var that = this;
    var dis = that.data.dis;
    that.setData({
      dis: false
    })
  },
  //  提交
  formSubmit: function (e) {
    console.log(e);
    var that = this;
    var isconfirm = that.data.isconfirm;
    console.log(that.data.isconfirm);
    var user_value = e.detail.value;
    if (user_value.user_name == '') {
      gift('请输入联系人');
      return;
    }
    if (user_value.user_phone == '') {
      gift('请输入电话号码');
      return;
    }
    if (user_value.province == '') {
      gift('请输入省');
      return;
    }
    if (user_value.city == '') {
      gift('请输入市');
      return;
    }
    if (user_value.area == '') {
      gift('请输入区');
      return;
    }
    if (user_value.use_address == '') {
      gift('请输入地址');
      return;
    }
    function gift(text) {
      //提示信息
      wx.showToast({
        title: text,
        icon: 'loading',
        duration: 1500,
      })
    }
    console.log('恭喜兑换成功');
    wx.request({
      url: app.url + 'api/goods/exchange',
      data: {
        openid: wx.getStorageSync('openid'),
        gid: that.data.gid,
        use_name: user_value.user_name,
        use_phone: user_value.user_phone,
        province: user_value.province,
        city: user_value.city,
        area: user_value.area,
        use_address: user_value.user_address,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data);
        var data = res.data;
        that.setData({
          dis: false
        });
        if (data.status == 1) {
          // wx.showToast({
          //   title: data.msg,
          //   icon: 'success',
          //   duration: 1500,
          // })
          wx.showModal({
            title: '兑换成功',
            content: data.msg,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'loading',
            duration: 1500,
          })
        }
      }
    })

  },

  onLoad: function (options) {
    wx.showLoading({ title: 'loading', mask: true })
    var that = this;
    that.setData({ gid: options.id });
    wx.request({
      url: app.url + 'api/goods/index',
      data: {
        openid: wx.getStorageSync('openid'),
        id: options.id,
        // token: wx.getStorageSync('token_md5'),
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        that.setData({
          banners: data,
          talks: data[0].explain,
        })
        wx.hideLoading();
      }
    })
  },
})